<template>
  <div v-if="type === 'bpmn:TimerEventDefinition'" :type="refreshForm">
    <c-form :inline="true" :form="form" ref="refForm" ></c-form>
  </div>
  <div v-if="type === 'bpmn:MessageEventDefinition'" :type="refreshForm">
    <c-form :inline="true" :form="form" ref="refForm" ></c-form>
  </div>
</template>

<script>
import Form from "@/components/Form.vue";
import {timerEventForm, MessageIntermediateCatchEventForm} from "@/components/bpmn/components/bpmnPropertiesPanel/forms";
import {getTimerDefinitionType} from "@/bpmn/util/common";

export default {
  name: "Properties_IntermediateCatchEvent",
  components: {
    "c-form": Form
  },
  computed: {
    type() {
      if (this?.bpmnElement?.businessObject?.eventDefinitions) {
        var __this = this;
        return this.bpmnElement.businessObject.eventDefinitions[0].$type;
      }
      return null;
    },
    refreshForm() {
      if (this?.bpmnElement?.businessObject?.eventDefinitions) {
        var __this = this;
        __this.form = this.getForm();
        return this.bpmnElement.businessObject.eventDefinitions[0].$type;
      }
      return null;
    }
  },
  data() {
    return {
      bpmnElement: null,
      form: null
    }
  },
  methods: {
    //获取表单
    getForm() {
      if (this.type === "bpmn:TimerEventDefinition") {
        return timerEventForm.call(this);
      } else if (this.type === 'bpmn:MessageEventDefinition') {
        return MessageIntermediateCatchEventForm.call(this);
      }
      return MessageIntermediateCatchEventForm.call(this);
    },
    // 数据回填
    dataReset() {
      if (this.type === "bpmn:TimerEventDefinition") {
        const modeler = this.modeler;
        let eventDefinitions = modeler.getProperty("eventDefinitions")
        if(eventDefinitions){
          eventDefinitions = eventDefinitions[0];
          const type = getTimerDefinitionType(eventDefinitions);
          const definition = eventDefinitions.get(type);
          if(definition){
            const body = definition.get('body');
            this.form.models.type = type;
            this.form.models.content = body;
          }
        }
      } else if (this.type === 'bpmn:MessageEventDefinition') {
        const modeler = this.modeler;
        let eventDefinitions = modeler.getProperty("eventDefinitions")
        if (eventDefinitions) {
          const definition = eventDefinitions[0];
          const messageRef = definition.get("messageRef");
          if (definition && messageRef) {
            const messageId = messageRef.get("name");
            this.form.models.name = messageId;
          }
        }
      }
    }
  },
}
</script>

<style scoped>

</style>